<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { queryApiPersonage } from '@/api/personage'
import { ElMessage } from 'element-plus'

// 用户信息数据
const userInfo = ref({
    username:'',
    name:'',
    img:'',
    phone:'',
    createdAt:'',
    lastLogin:'',
    gneder:''
}

)

// 借阅统计
const borrowStats = ref({
  total: 15,
  current: 3,
  overdue: 0
})

// 最近借阅记录
const recentBorrows = ref([
  { id: 1, bookTitle: 'Vue.js设计与实现', borrowDate: '2024-01-05', returnDate: '2024-02-05', status: '借阅中' },
  { id: 2, bookTitle: 'TypeScript实战', borrowDate: '2023-12-20', returnDate: '2024-01-20', status: '已归还' },
  { id: 3, bookTitle: '算法导论', borrowDate: '2023-11-15', returnDate: '2023-12-15', status: '已归还' }
])

// 编辑模式
const isEditing = ref(false)

// 保存个人信息
const saveProfile = () => {
  // 这里可以添加保存逻辑
  isEditing.value = false
  console.log('保存用户信息:', userInfo.value)
}

// 取消编辑
const cancelEdit = () => {
  isEditing.value = false
  // 可以添加重置数据逻辑
}

// 组件挂载时获取数据
onMounted(async() => {
    const reuslt = await queryApiPersonage()
    if(reuslt){
        userInfo.value = reuslt.data
    }
})
</script>

<template>
  <div class="personal-center">
    <h1>个人中心</h1>
    
    <div class="profile-section">
      <div class="profile-header">
        <div class="avatar-container">
          <img :src="userInfo.img" :alt="userInfo.name" class="avatar">
        </div>
        <div class="user-info">
          <h2>{{ userInfo.name }}</h2>
          <p class="user-id">用户名: {{ userInfo.username }}</p>
          <p class="join-date">注册时间: {{ userInfo.createdAt }}</p>
          <p class="last-login">最后登录: {{ userInfo.lastLogin }}</p>
        </div>
      </div>
      
      <div class="profile-details">
        <h3>基本信息</h3>
        <div class="info-item">
          <span class="label">姓名:</span>
          <span v-if="!isEditing">{{ userInfo.name }}</span>
          <input v-else v-model="userInfo.name" type="text">
        </div>

        <div class="info-item">
          <span class="label">性别:</span>
          <span v-if="!isEditing">{{ userInfo.gneder }}</span>
          <input v-else v-model="userInfo.gneder" type="text">
        </div>
        
        <div class="info-item">
          <span class="label">用户名:</span>
          <span v-if="!isEditing">{{ userInfo.username }}</span>
          <input v-else v-model="userInfo.username" >
        </div>
        
        <div class="info-item">
          <span class="label">手机号:</span>
          <span v-if="!isEditing">{{ userInfo.phone }}</span>
          <input v-else v-model="userInfo.phone" type="tel">
        </div>
        
        <div class="button-group">
          <button v-if="!isEditing" @click="isEditing = true" class="edit-btn">编辑信息</button>
          <template v-else>
            <button @click="saveProfile" class="save-btn">保存</button>
            <button @click="cancelEdit" class="cancel-btn">取消</button>
          </template>
        </div>
      </div>
    </div>
    
    <div class="stats-section">
      <h3>借阅统计</h3>
      <div class="stats-container">
        <div class="stat-item">
          <p class="stat-number">{{ borrowStats.total }}</p>
          <p class="stat-label">累计借阅</p>
        </div>
        <div class="stat-item">
          <p class="stat-number">{{ borrowStats.current }}</p>
          <p class="stat-label">当前借阅</p>
        </div>
        <div class="stat-item">
          <p class="stat-number">{{ borrowStats.overdue }}</p>
          <p class="stat-label">未归还数量</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.personal-center {
  padding: 15px;
  max-width: 1200px;
  margin: 0 auto;
}

.profile-section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
  margin-bottom: 24px;
}

.profile-header {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
  margin-bottom: 20px;
}

.avatar-container {
  margin-right: 30px;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #f0f0f0;
}

.user-info h2 {
  margin: 0 0 10px 0;
  font-size: 24px;
  color: #333;
}

.user-info p {
  margin: 8px 0;
  color: #666;
}

.profile-details h3 {
  margin-top: 0;
  color: #333;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.label {
  width: 80px;
  font-weight: bold;
  color: #555;
}

.info-item input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-left: 10px;
}

.button-group {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.edit-btn, .save-btn, .cancel-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 10px;
  font-size: 14px;
}

.edit-btn {
  background-color: #409eff;
  color: white;
}

.save-btn {
  background-color: #67c23a;
  color: white;
}

.cancel-btn {
  background-color: #909399;
  color: white;
}

.stats-section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
  margin-bottom: 24px;
}

.stats-section h3 {
  margin-top: 0;
  color: #333;
}

.stats-container {
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.stat-item {
  flex: 1;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  margin: 0;
  color: #409eff;
}

.stat-label {
  margin: 5px 0 0;
  color: #666;
  font-size: 14px;
}

.borrow-history {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
}

.borrow-history h3 {
  margin-top: 0;
  color: #333;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.history-item {
  padding: 15px;
  border-radius: 6px;
  border-left: 4px solid #ddd;
}

.history-item.borrowing {
  border-left-color: #409eff;
  background-color: #ecf5ff;
}

.history-item.returned {
  border-left-color: #67c23a;
  background-color: #f0f9eb;
}

.book-title {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 8px;
  color: #333;
}

.borrow-info {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #666;
}

.status {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.status.active {
  background-color: #409eff;
  color: white;
}

.status.inactive {
  background-color: #67c23a;
  color: white;
}

@media (max-width: 768px) {
  .profile-header {
    flex-direction: column;
    text-align: center;
  }
  
  .avatar-container {
    margin-right: 0;
    margin-bottom: 20px;
  }
  
  .borrow-info {
    flex-direction: column;
    gap: 5px;
  }
  
  .stats-container {
    flex-direction: column;
    gap: 20px;
  }
}
</style>